import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.JPanel;

public abstract class _Caddy
{
    ContentPanel mainPanel = new ContentPanel();
    public ArrayList<_GraphicElement> objects = new ArrayList<_GraphicElement>();
    public ArrayList<Drawable> drawings = new ArrayList<Drawable>();
    protected double earthAcceleration = -9.810000000000001D;
    protected double stepTime = 1.0D;
    processThread pd;
    public int breite;
    public int hohe;

    public _Caddy()
    {
        this.pd = new processThread();
        this.pd.start();
    }

    public void addPanelTo(Container c) {
        c.add(this.mainPanel);
    }

    public void addObject(_GraphicElement gE) {
        this.objects.add(gE);
        this.drawings.add(gE);
    }
    public void addObject(Drawable d) {
        this.drawings.add(d);
    }

    public void deleteObejct(_GraphicElement gE) {
        for(int i = 0; i < objects.size(); i++)
        {
            if(objects.get(i)== gE) objects.remove(i);
        }
        for(int i = 0; i < drawings.size(); i++)
        {
            if(drawings.get(i)== gE) drawings.remove(i);
        }
    }

    public double getStepTime() {
        return this.stepTime;
    }

    public double earthAcc() {
        return this.earthAcceleration;
    }

    public void adjustStepTime(double t) {
        this.stepTime = t;
    }

    abstract double getVX();

    abstract double getVY();
    
    public abstract void update();

    public void paintMyPanel(Graphics g)
    {
        for (int i = 0; i < this.drawings.size(); i++)
        {
            drawings.get(i).paint(g);
        }
    }

    class ContentPanel extends JPanel
    {
        ContentPanel()
        {
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            _Caddy.this.paintMyPanel(g);
        }
    }

    private class processThread extends Thread
    {
        private processThread()
        {
        }

        public void run()
        {
            while (true)
            {
                update();
                for (int i = 0; i < _Caddy.this.objects.size(); i++)
                {
                    _GraphicElement gE = (_GraphicElement)_Caddy.this.objects.get(i);
                    gE.updatePosition();
                }
                try
                {
                    sleep(20);
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}